﻿using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Neo4JConnector
{
    public class NodeProperty : ObjectPropertyBase
    {
        [FormulaProperty]
        [DisplayName("属性名")]
        public object Name { get; set; }

        [DisplayName("值")]
        [FormulaProperty(AcceptsReturn = true)]
        public object Value { get; set; }

        public static async Task<Dictionary<string, object>> ToPropertyDictionary(IServerCommandExecuteContext dataContext, List<NodeProperty> properties)
        {
            var dict = new Dictionary<string, object>();
            if (properties == null) return dict;
            foreach (var prop in properties)
            {

                var name = (await dataContext.EvaluateFormulaAsync(prop.Name))?.ToString() ?? "";
                if (string.IsNullOrEmpty(name)) continue;
                if (dict.ContainsKey(name))
                {
                    throw new Exception($"属性名 '{name}' 重复，请确保每个属性名唯一。");
                }

                var val = await dataContext.EvaluateFormulaAsync(prop.Value);

                dict[name] = val;
            }
            return dict;
        }
    }
}
